#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk, subprocess, sys
import egg.trayicon


def exit_applet( *args ):
	gtk.main_quit();
	return;

def run_phctool( *args ):
	cmd=[sys.path[0]+"/phctool.sh"]		##command
	subprocess.Popen(cmd, shell=False)	##call the affinated programm

def applet_face_click( window, event, *data ):
	if event.button == 3 :
		menu.popup( None, None, None, 0, event.time );
	elif event.button == 1 :
		run_phctool(None)
	return;


event_box = gtk.EventBox( );
image_widget = gtk.Image( );
image_widget.set_from_file(sys.path[0]+"/inc/phc_icon_22.png");

event_box.set_events( gtk.gdk.BUTTON_PRESS_MASK |
    gtk.gdk.POINTER_MOTION_MASK |
    gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.CONFIGURE );

event_box.add( image_widget );
event_box.connect("button_press_event", applet_face_click );

menu_items = (
	(sys.path[0]+"/inc/phc_icon_22.png", "Open PHCtool", run_phctool ),
	None,
	(sys.path[0]+"/inc/phc_exit_22.png", "Exit Tray", exit_applet )
	);

menu = gtk.Menu()
for i in menu_items:
    if i is None:
        menu.add(gtk.SeparatorMenuItem());
    else:
        menu_items = gtk.ImageMenuItem( i[1] );
        menu_items.connect( 'activate', i[2] );

	image = gtk.Image()
	image.set_from_file( i[0] )
	menu_items.set_image( image );

        menu.add( menu_items );


menu.show_all()

t = egg.trayicon.TrayIcon("PHCtoolTray")
t.add(event_box)
t.show_all()

##call phctool without GUI to set stored values
cmd=[sys.path[0]+"/phctool.sh","background"]	##command
subprocess.Popen(cmd, shell=False)		##call the affinated programm

gtk.main()


